package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NLaboratoryPenetrationBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryPenetrationVo;
import com.ruoyi.zhgd.domain.NLaboratoryPenetration;
import com.ruoyi.zhgd.mapper.NLaboratoryPenetrationMapper;
import com.ruoyi.zhgd.service.INLaboratoryPenetrationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 针入度试验Service业务层处理
 *
 * @author zyb
 * @date 2023-09-11
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NLaboratoryPenetrationServiceImpl implements INLaboratoryPenetrationService {

    private final NLaboratoryPenetrationMapper baseMapper;

    /**
     * 查询针入度试验
     */
    @Override
    public NLaboratoryPenetrationVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询针入度试验列表
     */
    @Override
    public TableDataInfo<NLaboratoryPenetrationVo> queryPageList(NLaboratoryPenetrationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NLaboratoryPenetration> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryPenetrationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询针入度试验列表
     */
    @Override
    public List<NLaboratoryPenetrationVo> queryList(NLaboratoryPenetrationBo bo) {
        LambdaQueryWrapper<NLaboratoryPenetration> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NLaboratoryPenetration> buildQueryWrapper(NLaboratoryPenetrationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NLaboratoryPenetration> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOrganizationId()), NLaboratoryPenetration::getOrganizationId, bo.getOrganizationId());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceId()), NLaboratoryPenetration::getDeviceId, bo.getDeviceId());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectId()), NLaboratoryPenetration::getProjectId, bo.getProjectId());
        lqw.eq(StringUtils.isNotBlank(bo.getEngineeringSite()), NLaboratoryPenetration::getEngineeringSite, bo.getEngineeringSite());
        lqw.eq(StringUtils.isNotBlank(bo.getSampleNo()), NLaboratoryPenetration::getSampleNo, bo.getSampleNo());
        lqw.like(StringUtils.isNotBlank(bo.getSampleName()), NLaboratoryPenetration::getSampleName, bo.getSampleName());
        lqw.eq(bo.getStartTime() != null, NLaboratoryPenetration::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, NLaboratoryPenetration::getEndTime, bo.getEndTime());
        lqw.eq(bo.getUploadTime() != null, NLaboratoryPenetration::getUploadTime, bo.getUploadTime());
        lqw.eq(bo.getInsertTime() != null, NLaboratoryPenetration::getInsertTime, bo.getInsertTime());
        lqw.eq(bo.getValue1() != null, NLaboratoryPenetration::getValue1, bo.getValue1());
        lqw.eq(bo.getValue2() != null, NLaboratoryPenetration::getValue2, bo.getValue2());
        lqw.eq(bo.getValue3() != null, NLaboratoryPenetration::getValue3, bo.getValue3());
        lqw.eq(bo.getAverage() != null, NLaboratoryPenetration::getAverage, bo.getAverage());
        lqw.eq(StringUtils.isNotBlank(bo.getIsQualified()), NLaboratoryPenetration::getIsQualified, bo.getIsQualified());
        lqw.eq(bo.getMinValue() != null, NLaboratoryPenetration::getMinValue, bo.getMinValue());
        lqw.eq(StringUtils.isNotBlank(bo.getBaseCode()), NLaboratoryPenetration::getBaseCode, bo.getBaseCode());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptPkid()), NLaboratoryPenetration::getDeptPkid, bo.getDeptPkid());
        lqw.eq(bo.getMaxValue() != null, NLaboratoryPenetration::getMaxValue, bo.getMaxValue());
        lqw.eq(bo.getTemperature() != null, NLaboratoryPenetration::getTemperature, bo.getTemperature());
        lqw.eq(StringUtils.isNotBlank(bo.getTestType()), NLaboratoryPenetration::getTestType, bo.getTestType());
        lqw.like(StringUtils.isNotBlank(bo.getEngineeringName()), NLaboratoryPenetration::getEngineeringName, bo.getEngineeringName());
        return lqw;
    }

    /**
     * 新增针入度试验
     */
    @Override
    public Boolean insertByBo(NLaboratoryPenetrationBo bo) {
        NLaboratoryPenetration add = BeanUtil.toBean(bo, NLaboratoryPenetration.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改针入度试验
     */
    @Override
    public Boolean updateByBo(NLaboratoryPenetrationBo bo) {
        NLaboratoryPenetration update = BeanUtil.toBean(bo, NLaboratoryPenetration.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NLaboratoryPenetration entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除针入度试验
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
